﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.ModelBinding;

namespace DoeLibsMVC.Filter
{
    public class FilterHelper
    {
        /// <summary>
        /// makes the ModelState errors in a nicer way (hashmap with fieldname as key and errors as list of the errors)
        /// </summary>
        /// <param name="pModelState"></param>
        /// <returns></returns>
        public static Dictionary<string, IEnumerable<string>> makeErrorsNice(ModelStateDictionary pModelState)
        {
            // Return the validation errors in the response body.
            var errors = new Dictionary<string, IEnumerable<string>>();
            //string key;
            foreach (KeyValuePair<string, ModelState> keyValue in pModelState)
            {
                errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
            }

            return errors;
        }
    }
}